package com.chezai.client.utils;

import android.annotation.SuppressLint;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.orhanobut.logger.Logger;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@SuppressLint("SimpleDateFormat")
public class DateUtil {
    private static String sf1 = "yyyy年MM月dd日";
    private static String sf2 = "yyyy-MM-dd";
    public static String sf3 = "yyyy-MM-dd  HH:mm:ss";
    public static String sf4 = "HH:mm";
    public static String sf5 = "yyyy-MM-dd-HH";
    public static String sf6 = "yyMMddHHmm";
    public static String sf7 = "yyyy-MM-dd HH:mm:ss";
    public static String sf28 = "yyMMdd";
    public static String sf18 = "yyyy/MM/dd";
    public static String sf19 = "yyMMddHHmmss";
    public static String sf20 = "MMddHHmmss";
    /**
     * 获取过去第几天的日期
     *
     * @param past
     * @return
     */
    public static String getPastDate(int past,String formatStr) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        String result = format.format(today);
        return result;
    }

    private static String timestampToStr(String format, Date timestamp) {
        DateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(timestamp);
    }

    public static String timestampStr(String formatStr) {
        return timestampStr(System.currentTimeMillis(), formatStr);
    }

    public static String timestampStr(@NonNull Long timestamp, @NonNull String formatStr) {
        if (TextUtils.isEmpty(formatStr)) {
            return "";
        }
        if (timestamp == null) {
            return "";
        }
        Date date = new Date(timestamp);
        return timestampToStr(formatStr, date);
    }

    public static String timestampStr(Long timestamp) {
        Date date = new Date(timestamp);
        return timestampToStr(sf1, date);
    }

    public static String timestampStr2(Long timestamp) {
        Date date = new Date(timestamp);
        return timestampToStr(sf2, date);
    }


    public static String timestampStr7(Long timestamp) {
        Date date = new Date(timestamp);
        return timestampToStr(sf7, date);
    }

    public static long stringTimeToLong(String time,String pattern) {
        long millionSeconds = 0;//毫秒
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            millionSeconds = sdf.parse(time).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return millionSeconds;
    }

    /**
     * 获取当前日期是星期几<br>
     *
     * @param dt
     * @return 当前日期是星期几
     */
    public static String getWeekOfDate(Date dt) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    public static int getTeachAge(String beginTime) {
        int year = 0;
        SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
        long nowTimeStamp = System.currentTimeMillis();
        try {
            String todayData = formatDate.format(nowTimeStamp);
            double difMon = compareDate(beginTime, todayData, 2);
            year = (int) difMon;
        } catch (Exception e) {
        }
        return year;

    }

    /**
     * @param date1 需要比较的时间 不能为空(null),需要正确的日期格式
     * @param date2 被比较的时间 为空(null)则为当前时间
     * @param stype 返回值类型 0为多少天，1为多少个月，2为多少年
     * @return
     */
    public static double compareDate(String date1, String date2, int stype) {
        int n = 0;

        String[] u = {"天", "月", "年"};
        String formatStyle = (stype == 1 ? "yyyy-MM" : "yyyy-MM-dd");
        DateFormat df = new SimpleDateFormat(formatStyle);
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        try {
            c1.setTime(df.parse(date1));
            c2.setTime(df.parse(date2));
        } catch (Exception e3) {
            Logger.i("wrong occured");
        }
        while (!c1.after(c2)) {
            // 循环对比，直到相等，n 就是所要的结果
            n++;
            if (stype == 1) {
                c1.add(Calendar.MONTH, 1); // 比较月份，月份+1
            } else {
                c1.add(Calendar.DATE, 1); // 比较天数，日期+1
            }
        }

        n = n - 1;

        if (stype == 2) {
            //如果不能够一年等于1年
            if (n <= 365) {
                n = 1;
            } else {
                n = n / 365;
            }
        }

        Logger.i(date1 + " -- " + date2 + " 相差多少" + u[stype] + ":"
                + n);
        return n;
    }

    /**
     * 得到当前日期
     *
     * @return
     */
    public static String getCurrentDate() {
        Calendar c = Calendar.getInstance();
        Date date = c.getTime();
        SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");
        return simple.format(date);
    }

    /**
     * 获取与当前时间的时间差
     * 如果传入的时间比当前时间大，返回0
     *
     * @param date
     * @return 分钟
     */
    public static String getTimeDifference(String date) {
        try {
            DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm");
            Date now = df.parse(date);
            long time = now.getTime();
            long currentTime = System.currentTimeMillis();

            if (time > currentTime) {
                return "0";
            } else {
                long l = currentTime - time;
                long minutes = TimeUnit.MILLISECONDS.toMinutes(l);
                return String.valueOf(minutes);
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return "0";
        }
    }

    /**
     * 获取时间差
     *
     * @param d1
     * @param d2
     * @return 分钟
     */
    public static long getTimeDifference(String d1, String d2) {
        try {
            DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm");
            Date date1 = df.parse(d1);
            Date date2 = df.parse(d2);
            long time1 = date1.getTime();
            long time2 = date2.getTime();
            return getTimeDifferenceMinutes(time1, time2);
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 获取时间差
     *
     * @param d1
     * @param d2
     * @return 分钟
     */
    public static long getTimeDifferenceMinutes(Long d1, Long d2) {
        long l;
        if (d1 > d2) {
            l = d1 - d2;
        } else {
            l = d2 - d1;
        }
        long minutes = TimeUnit.MILLISECONDS.toMinutes(l);
        return minutes;
    }

    /**
     * 返回时间差
     *
     * @param d1
     * @param d2
     * @return 数组第一位 位分钟数  第二位为秒
     */
    public static long[] getTimeDifference(long d1, long d2) {
        long[] result = new long[2];
        long minutes = getTimeDifferenceMinutes(d1, d2);
        result[0] = minutes;
        long l;
        if (d1 > d2) {
            l = d1 - d2;
        } else {
            l = d2 - d1;
        }
        long secondes = l / 1000 - minutes * 60;
        result[1] = secondes;
        return result;
    }

}
